import Configurable
import Item
import pygame
from pygame.locals import *
import Timekeeper
import G
import time
import Util

class Shield(Item.Item, Timekeeper.Timekeeper):
   runtime_vars = ('HP',
                   'recharge_timer',
                   'recharge_buffer',
                   'sprite',
                   'recharging')

   __slots__ = ('health',
                'recharge_delay',
                'recharge_rate',
                'color',
                'maintain_power',
                'recharge_power') + runtime_vars
   
   def __init__(self, health, recharge_rate, maintain_power, recharge_power, recharge_delay=0.0,
                color=(160,224,255), **args):
      Item.Item.__init__(self, **args)
      Configurable.Configurable.__init__(self, locals())
      self.sprite = pygame.sprite.Sprite()
      self.HP = self.health
      self.recharge_timer = 0.0
      self.recharge_buffer = 0.0
      self.recharging = False
   
   def augmented_description(self):
      return \
         "%s\nCost: %d\nHealth: %s Recharge Rate: %s Recharge_Delay: %.1f\nMaintain Power: %s Recharge Power: %.1f\n\n%s" % \
         (self.name, self.value, str(self.health).ljust(5), ("%s / sec" % str(self.recharge_rate)).ljust(11), self.recharge_delay,
          str(self.maintain_power).ljust(5), self.recharge_power, self.description)
   
   def damage(self, quantity, type):
      hurts = min(self.HP, quantity)
      self.HP -= hurts
      if self.HP < 0:
         self.HP = 0
      self.recharge_timer = self.recharge_delay
      return hurts
  
   def equip(self):
      rr = 2 * int(Util.rectradius(self.holder.rect)) + 4
      self.sprite.image = pygame.Surface((rr, rr))
      self.sprite.rect = self.sprite.image.get_rect()
      self.sprite.rect.center = [self.holder.rect.centerx, self.holder.rect.centery]
      self.sprite.image.set_colorkey((0,0,0), RLEACCEL)
      G.shields.add(self.sprite)
   
   def kill(self):
      self.sprite.kill()
   
   def update(self):
      frame_time = self.frame()
      recharged = False
      if self.recharge_timer != 0.0:
         self.recharge_timer -= frame_time
         if self.recharge_timer < 0.0:
            self.recharge_timer = 0.0
      elif self.HP < self.health:
         if not self.recharging:
            self.recharging = True
         self.recharge_buffer += self.recharge_rate * frame_time
         recharge, self.recharge_buffer = divmod(self.recharge_buffer, 1.0)
         needed = self.recharge_power * frame_time
         drained = self.holder.drain(needed)
         if drained < needed:
            recharge *= drained / needed
         self.HP += int(recharge)
         if self.HP >= self.health:
            self.HP = self.health
            self.recharging = False
         recharged = True
      if not recharged:
         needed = self.maintain_power * frame_time
         drained = self.holder.drain(needed)
         if drained < needed:
            self.damage(self.recharge_rate * frame_time * (1.0 - drained/needed), -1)
      self.sprite.rect.center = [self.holder.rect.centerx+2, self.holder.rect.centery+4]
      halfwid = self.sprite.rect.width/2
      pygame.draw.circle(self.sprite.image, self.color, (halfwid, halfwid), halfwid)
      self.sprite.image.set_alpha(int(128.0 * self.HP / self.health))
      
